package com.yilife.hmstr.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yilife.hmstr.domain.*;
import com.yilife.hmstr.mapper.HmstrRecordMapper;
import com.yilife.hmstr.service.HmstrRecordService;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 经络分析业务实现类
 */

@Service
public class HmstrRecordServiceImpl extends ServiceImpl<HmstrRecordMapper, HmstrRecord> implements HmstrRecordService{
	
	@Autowired
	private FiveElementsReportServiceImpl ferImpl;
	
	@Autowired
	private MeridianAnalysisServiceImpl maImpl;
	
	@Autowired
	private MeridianTableServiceImpl mtImpl;
	
	@Autowired
	private TestReportServiceImpl teImpl;

	@Autowired
	private HmstrRecordMapper hmstrRecordMapper;

	@Override
	public boolean deleteAll() {
		return retBool(baseMapper.deleteAll());
	}
	
	/*
	public String selectByID(int id){
		String string="";
		string=string+hrMapper.selectById(id).toString()+"\n\n";
		string=string+ferImpl.selectByPid(id);
		string=string+maImpl.selectByPid(id);
		string=string+mtImpl.selectByPid(id);
		string=string+teImpl.selectByPid(id);
		return string;
	}*/

	/**
	 * 根据主键查询数据，返回Hmstr对象
	 * @param id
	 * @return
	 */
	public Hmstr selectByID(long id){
//		HmstrRecord hmstrRecord=new HmstrRecord();
		if(id!=0){
			return new Hmstr(teImpl.selectByPid(id), ferImpl.selectByPid(id), maImpl.selectByPid(id), mtImpl.selectByPid(id),hmstrRecordMapper.selectById(id));
		}
		return null;
	}

	/**
	 * 根据传入的文件路径读取文件并解析
	 * @param filePath
	 * @return
	 */
	public Hmstr HtmlToObject(String filePath){
		HmstrRecord hmstrRecord=new HmstrRecord();
		tr tr=new tr();
		mt mt=new mt();
		ma ma=new ma();
		FiveElementsReport fer=null;
		String filenameRegex="[\\u4e00-\\u9fa5]{3,4}";//匹配中文字符的正则表达式
		String id="[1-9]\\d*";//匹配id的正则表达式
		String filename="";
		Long hmstrId=(long)0;
		Matcher matcher= Pattern.compile(filenameRegex).matcher(filePath);
		if (matcher.find()){
			filename=matcher.group();//获取健康报告类型
			//System.out.print(filename);
		}
		Matcher matcher1=Pattern.compile(id).matcher(filePath);
		if(matcher1.find()) {
			hmstrId = Long.valueOf(matcher1.group());//获取健康报告id
//			System.out.print("id是：：：：：：：：：："+hmstrId);
		}
		hmstrRecord.setHmstrId(hmstrId);
		hmstrRecord.setUserId(11);
//		System.out.print("是否为空"+hmstrRecordMapper.selectById(hmstrId));
		/**
		 * hmstrRecord不为空且hmstrRecord的主键在不存在于数据库，则进行数据插入操作
		 */
		if(hmstrRecord!=null&&hmstrRecordMapper.selectById(hmstrId)==null){
			hmstrRecordMapper.insert(hmstrRecord);
		}else{
			//hmstrRecordMapper.updateById(hmstrRecord);
		}


		Document document= HmstrParseUtil.ReadFile(filePath);
		//判断报告类型,根据报告类型进行解析并插入
		if(filename.equals("检测报告")){
			tr= HmstrParseUtil.ParseTestReport(document,hmstrId);
//			System.out.print(tr.toString());
			teImpl.add(tr.getTestReport(),tr.getMeridianChangeAnalysis(),tr.getTrProblems());
		}else if(filename.equals("经络表")){
			mt= HmstrParseUtil.ParseMeridianTable(document,hmstrId);
			mtImpl.add(mt.getMeridianTable(),mt.getFiveJudgmentSystems(),mt.getMeridianDatas());
		}else if(filename.equals("经络分析")){
			ma= HmstrParseUtil.ParseMeridianAnalysis(document,hmstrId);
			for(MeridianAnalysis meridianAnalysis:ma.getMeridianAnalysis()){
				maImpl.add(meridianAnalysis);
			}
		}else if(filename.equals("五行图")){
			fer= HmstrParseUtil.ParseFiveElementsReport(document,hmstrId);
			ferImpl.add(fer);
		}else if(filename.equals("美容报告")){

		}else if(filename.equals("生理症状")){

		}else if(filename.equals("柱状图")){

		}else if(filename.equals("专家建议")){

		}else{}
		hmstrRecord.setHmstrId(hmstrId);
		new Hmstr(tr,fer,ma,mt,hmstrRecord);
		return selectByID(hmstrId);
//		return new Hmstr(tr,fer,ma,mt,hmstrRecord);
//		return "无法解析该文件";

	}

	/**
	 *根据主键删除数据，先把各字表删除
	 * @param hmstrId
	 * @return
	 */
	public int delById(long hmstrId){
		ferImpl.delByPid(hmstrId);
		maImpl.delByPid(hmstrId);
		mtImpl.delByPid(hmstrId);
		teImpl.delByPid(hmstrId);
		return hmstrRecordMapper.deleteById(hmstrId);
	}

//	public static void main(String[] args){
//		new HmstrRecordServiceImpl().HtmlToObject("F:\\Java\\java\\javaweb\\hmstr\\upload\\13602896904(检测报告).htm");
//	}
}
